import {hasChange, isObject} from "../utils";
import {reactive} from "./reactive";
import {track, trigger} from "./effect";

export function ref(value){
    if(isRef(value)){
        return value;
    }
    return new RefImpl(value);
}

export function isRef(value){
    return !!(value && value.__isRef);
}

class RefImpl {
    constructor(value) {
        this.__isRef = true;
        this._value = convert(value);
    }
    get value(){
        track(this, 'value');
        return this._value;
    }
    set value(newValue){
        if (hasChange(newValue, this._value)) {
            this._value = newValue;
            trigger(this, 'value');
        }
    }
}
/**
* 可以接收普通值 和 对象
* @param value 值
*/
function convert(value){
    return isObject(value) ? reactive(value) : value;
}